/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.fee.core.service;

import info.peigen.hotpot.business.fee.facade.command.FeeRuleServiceFacade;
import info.peigen.hotpot.business.fee.facade.command.order.rule.FeeRuleCreateOrder;
import info.peigen.hotpot.business.fee.facade.command.order.rule.FeeRuleDeleteOrder;
import info.peigen.hotpot.business.fee.facade.command.order.rule.FeeRuleModifyOrder;
import info.peigen.hotpot.business.fee.facade.command.result.FeeRuleCreateResult;
import info.peigen.hotpot.business.fee.facade.command.result.FeeRuleDeleteResult;
import info.peigen.hotpot.business.fee.facade.command.result.FeeRuleModifyResult;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceInvoke;
import org.noear.solon.annotation.Component;


/**
 * <b>(FeeRuleService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/11/10
 */
@Component
public class FeeRuleService extends AbstractFeeService implements FeeRuleServiceFacade {

    @Override
    public FeeRuleCreateResult createFeeRule(FeeRuleCreateOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new FeeRuleCreateResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addPreProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(checkTimeRangeOverride24HoursProcessor, checkDuplicateFeeRuleProcessor);
            }

            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(feeRuleCreateProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeFeeRuleProcessor, feeRuleCreateResultProcessor);
            }
        });
        return (FeeRuleCreateResult) serviceContext.getResult();
    }

    @Override
    public FeeRuleModifyResult modifyFeeRule(FeeRuleModifyOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new FeeRuleModifyResult()).build();
        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addPreProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(checkTimeRangeOverride24HoursProcessor);
            }

            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(feeRuleModifyProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(storeFeeRuleProcessor);
            }
        });
        return (FeeRuleModifyResult) serviceContext.getResult();
    }

    @Override
    public FeeRuleDeleteResult deleteFeeRule(FeeRuleDeleteOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new FeeRuleDeleteResult()).build();
        execute(serviceContext, closure -> closure.addProcessors(feeRuleDeleteProcessor));
        return (FeeRuleDeleteResult) serviceContext.getResult();
    }

}